package com.betterjr.modules.risk.loan.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import com.betterjr.common.annotation.BetterjrMapper;
import com.betterjr.mapper.common.Mapper;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;

@BetterjrMapper
public interface LoanProjectRelationMapper extends Mapper<LoanProjectRelation> {

    @Select("<script>"
            + "select id id, C_ELCAGGREMENT_NAME elcAggrementName,C_LOANAGGREMENT_NO loanAggrementNo, C_CUST_NAME custName,L_CUSTNO custNo,C_CUSTCODE custCode,C_CORE_NAME coreName ,L_CORENO coreNo ,C_FACTORY_NAME factoryName,L_FACTORYNO factoryNo,F_BALANCE balance,F_LOAN_BALANCE loanBalance,C_PAY_DATE payDate,C_PAY_TIME payTime,F_DISCOUNT discount,C_LOAN_DATE loanDate,C_BUSINSTATUS businStatus,C_CREATE_DATE createDate,C_CREATE_TIME createTime,C_CREATE_OPERATORNAME createOperatorName,N_CREATE_OPERATORID createOperatorId,C_AUDIT_DATE auditDate,C_AUDIT_TIME auditTime,C_AUDIT_OPERATORNAME auditOperatorName,N_AUDIT_OPERATORID auditOperatorId,C_FACTORYLOAN_DATE factoryLoanDate,C_FACTORYLOAN_TIME factoryLoanTime,C_FACTORYLOAN_OPERATORNAME factoryLoanOperatorName,N_FACTORYLOAN_OPERATORID  factoryLoanOperatorId,F_SERVICE_RATE serviceRate,C_WITHHOLD_RULE withHoldRule,N_FINANCINGMATURITY_DATE financingMaturityDate from t_scf_loanproject_relation "
            + " <where> "
            + " <if test='anQueryMap.LIKEelcAggrementName!=null '> or C_ELCAGGREMENT_NAME like #{anQueryMap.LIKEelcAggrementName} </if> "
            + " <if test='anQueryMap.LIKEloanAggrementNo!=null '> or C_LOANAGGREMENT_NO like #{anQueryMap.LIKEloanAggrementNo} </if> "
            + " <if test='anQueryMap.LIKEcustName!=null '> or C_CUST_NAME like #{anQueryMap.LIKEcustName} </if> "
            + " <if test='anQueryMap.LIKEcoreName!=null '> or C_CORE_NAME like #{anQueryMap.LIKEcoreName} </if> "
            + " <if test='anQueryMap.LIKEcustCode!=null '> or C_CUSTCODE like #{anQueryMap.LIKEcustCode} </if> "
            + " <if test='anQueryMap.LIKEfactoryName!=null '> or C_FACTORY_NAME like #{anQueryMap.LIKEfactoryName} </if> "
            + " <if test='anQueryMap.factoryNo!=null '> and L_FACTORYNO = #{anQueryMap.factoryNo} </if>"
            + " <if test='anQueryMap.coreNo!=null '> and L_CORENO = #{anQueryMap.coreNo} </if>"
            + " <if test='anQueryMap.custCode!=null '> and C_CUSTCODE = #{anQueryMap.custCode} </if>"
            + " <if test='anQueryMap.businStatus!=null and anQueryMap.businStatus.size>0 '> <foreach collection='anQueryMap.businStatus' open=' and C_BUSINSTATUS in (' close=')' item='busin' separator=','> #{busin} </foreach> </if> "
            + " </where> " + " <if test='orderBy!=null '> order by  #{orderBy} </if>  "
            + " limit #{startNum}, #{endNum} " + "</script>")
    @ResultType(List.class)
    public List<LoanProjectRelation> queryLoanRelationByPage(@Param("anQueryMap") Map<String, Object> anQueryMap,
            @Param("startNum") Integer startNum, @Param("endNum") Integer endNum, @Param("orderBy") String orderBy);

    @Select("<script>" + "select count(1) from t_scf_loanproject_relation " + " <where>  "
            + " <if test='anQueryMap.LIKEelcAggrementName!=null'> or C_ELCAGGREMENT_NAME like #{anQueryMap.LIKEelcAggrementName} </if> "
            + " <if test='anQueryMap.LIKEloanAggrementNo!=null '> or C_LOANAGGREMENT_NO like #{anQueryMap.LIKEloanAggrementNo} </if> "
            + " <if test='anQueryMap.LIKEcustName!=null '> or C_CUST_NAME like #{anQueryMap.LIKEcustName} </if> "
            + " <if test='anQueryMap.LIKEcoreName!=null '> or C_CORE_NAME like #{anQueryMap.LIKEcoreName} </if> "
            + " <if test='anQueryMap.LIKEcustCode!=null '> or C_CUSTCODE like #{anQueryMap.LIKEcustCode} </if> "
            + " <if test='anQueryMap.LIKEfactoryName!=null'> or C_FACTORY_NAME like #{anQueryMap.LIKEfactoryName} </if> "
            + " <if test='anQueryMap.factoryNo!=null '> and L_FACTORYNO = #{anQueryMap.factoryNo} </if>"
            + " <if test='anQueryMap.coreNo!=null '> and L_CORENO = #{anQueryMap.coreNo} </if>"
            + " <if test='anQueryMap.custCode!=null '> and C_CUSTCODE = #{anQueryMap.custCode} </if>"
            + " <if test='anQueryMap.businStatus!=null and anQueryMap.businStatus.size>0 '> <foreach collection='anQueryMap.businStatus' open=' and C_BUSINSTATUS in (' close=')' item='busin' separator=','> #{busin} </foreach> </if> "
            + " </where> " + "</script>")
    @ResultType(Long.class)
    public Long queryLoanRelationCount(@Param("anQueryMap") Map<String, Object> anQueryMap);

}
